package com.kws.tool.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.kws.common.core.utils.StringUtils;
import com.kws.system.api.domain.QuerySignUrlRq;
import com.kws.tool.dto.*;
import com.kws.tool.dto.info.*;
import com.kws.tool.enums.ElectronicSignatureUrlEnum;
import com.kws.tool.service.ElectronicSignatureService;
import com.kws.tool.util.ElectronicSignatureHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <b>ElectronicSignatureServiceImpl</b>
 *
 * @description: ElectronicSignatureServiceImpl <br>
 * @date: 2024/11/11 09:47 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class ElectronicSignatureServiceImpl implements ElectronicSignatureService {
    private static final Logger logger = LoggerFactory.getLogger(ElectronicSignatureServiceImpl.class);

    @Value("${electronicSignature.app_id}")
    private String appId;

    @Value("${electronicSignature.app_key}")
    private String appKey;

    @Override
    public QueryFileUploadUrlInfo getFileUploadUrl(QueryFileUploadRq rq) throws Exception {
        String requestJson = JSONUtil.toJsonStr(JSONUtil.parse(rq));
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpPostUtils(ElectronicSignatureUrlEnum.QUERY_FILE_UPLOAD_URL, appId, appKey, requestJson);
        logger.info("查询电子签章文件上传地址返回结果:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            QueryFileUploadUrlInfo info = JSONUtil.toBean(JSONUtil.parseObj(entries.get("data")), QueryFileUploadUrlInfo.class);
            return info;
        }
        return null;
    }

    @Override
    public QueryFileUploadStatusInfo queryFileUploadStatus(String fileId) throws Exception {
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpGetUtils(ElectronicSignatureUrlEnum.QUERY_FILE_UPLOAD_STATUS+fileId, appId, appKey, null);
        logger.info("查询电子签章文件上传状态返回结果:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            QueryFileUploadStatusInfo info = JSONUtil.toBean(entries, QueryFileUploadStatusInfo.class);
            return info;
        }
        return null;
    }

    @Override
    public QueryTemplateMakingInfo queryTemplateMaking(QueryTemplateMakingRq rq) throws Exception {
        String requestJson = JSONUtil.toJsonStr(JSONUtil.parse(rq));
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpPostUtils(ElectronicSignatureUrlEnum.QUERY_TEMPLATE_MAKING_URL, appId, appKey, requestJson);
        logger.info("查询电子签章创建合同文件模板返回结果:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            QueryTemplateMakingInfo info = JSONUtil.toBean(JSONUtil.parseObj(entries.get("data")), QueryTemplateMakingInfo.class);
            return info;
        }
        return null;

    }

    @Override
    public List<String> createControlGroup(CreateControlGroupRq rq) throws Exception {
        String requestJson = JSONUtil.toJsonStr(JSONUtil.parse(rq));
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpPostUtils(ElectronicSignatureUrlEnum.CREATE_CUSTOM_SERVICE_CONTROL, appId, appKey, requestJson);
        logger.info("查询电子签章创建控件组返回结果:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            List<String> info = JSONUtil.toBean(entries, List.class);
            return info;
        }
        return null;
    }

    @Override
    public QueryTemplateControlInfo queryTemplateControl(String docTemplateId) throws Exception {
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpGetUtils(ElectronicSignatureUrlEnum.QUERY_TEMPLATE_CONTROL_URL+docTemplateId, appId, appKey, null);
        logger.info("查询合同模板中控件详情返回结果:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            QueryTemplateControlInfo info = JSONUtil.toBean(JSONUtil.parseObj(entries.get("data")), QueryTemplateControlInfo.class);
            return info;
        }
        return null;
    }

    @Override
    public QueryTemplateFillInUrlInfo queryTemplateFillInUrl(QueryTemplateFillInUrlRq rq) throws Exception {
        String requestJson = JSONUtil.toJsonStr(JSONUtil.parse(rq));
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpPostUtils(ElectronicSignatureUrlEnum.QUERY_TEMPLATE_FILL_IN_URL, appId, appKey, requestJson);
        logger.info("查询电子签章创建控件组返回结果:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            QueryTemplateFillInUrlInfo info = JSONUtil.toBean(JSONUtil.parseObj(entries.get("data")), QueryTemplateFillInUrlInfo.class);
            return info;
        }
        return null;
    }

    @Override
    public QuerySignContractInfo querySignContract(QuerySignContractRq rq) throws Exception {
        String requestJson = JSONUtil.toJsonStr(JSONUtil.parse(rq));
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpPostUtils(ElectronicSignatureUrlEnum.QUERY_SIGN_CONTRACT, appId, appKey, requestJson);
        logger.info("发起合同签署返回结果:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            String signFlowId = JSONUtil.toJsonStr(entries.get("data"));
            QuerySignContractInfo info = new QuerySignContractInfo();
            if (StringUtils.isNotEmpty(signFlowId)){
                info.setSignFlowId(signFlowId);
            }
//            QuerySignContractInfo info = JSONUtil.toBean(JSONUtil.parseObj(entries.get("data")), QuerySignContractInfo.class);
            return info;
        }
        return null;
    }

    @Override
    public QuerySignUrlInfo getSignUrl(QuerySignUrlRq rq) throws Exception {
        String requestJson = JSONUtil.toJsonStr(JSONUtil.parse(rq));
        String url = "/v3/sign-flow/"+rq.getSignFlowId()+"/sign-url";
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpPostUtils(url, appId, appKey, requestJson);
        logger.info("获取签署页面连接返回结果:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            QuerySignUrlInfo info = JSONUtil.toBean(JSONUtil.parseObj(entries.get("data")), QuerySignUrlInfo.class);
            return info;
        }
        return null;
    }

    @Override
    public QuerySignPageInfo querySignPageUrl(QuerySignPageRq rq) throws Exception {
        QuerySignPageUrlRq querySignPageUrlRq = new QuerySignPageUrlRq();
        querySignPageUrlRq.setNeedLogin(rq.isNeedLogin());
        querySignPageUrlRq.setUrlType(rq.getUrlType());
        querySignPageUrlRq.setClientType(rq.getClientType());
        querySignPageUrlRq.setAppScheme(rq.getAppScheme());
        querySignPageUrlRq.setRedirectConfig(rq.getRedirectConfig());
        String requestJson = JSONUtil.toJsonStr(JSONUtil.parse(querySignPageUrlRq));
        String url = "/v3/sign-flow/"+rq.getSignFlowId()+"/sign-url";
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpPostUtils(url, appId, appKey, requestJson);
        logger.info("获取签署页面连接返回结果:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            QuerySignPageInfo info = JSONUtil.toBean(JSONUtil.parseObj(entries.get("data")), QuerySignPageInfo.class);
            return info;
        }
        return null;
    }

    @Override
    public QuerySigningProcessInfo querySigningProcess(String signFlowId) throws Exception {
        String url = "/v3/sign-flow/"+signFlowId+"/detail";
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpGetUtils(url, appId, appKey, null);
        logger.info("查询合同模板中控件详情返回结果:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            QuerySigningProcessInfo info = JSONUtil.toBean(JSONUtil.parseObj(entries.get("data")), QuerySigningProcessInfo.class);
            return info;
        }
        return null;
    }

    @Override
    public boolean completedSigningPProcess(String signFlowId) throws Exception {
        String url = "/v3/sign-flow/"+signFlowId+"/finish";
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpPostUtils(url, appId, appKey, null);
//        if (null != entries){
//            return true;
//        }
        return true;
    }

    @Override
    public DownloadSignContractInfo downloadSignContract(String signFlowId) throws Exception {
        String url = "/v3/sign-flow/"+signFlowId+"/file-download-url";
        JSONObject entries = ElectronicSignatureHttpUtil.electronicSignatureHttpGetUtils(url, appId, appKey, null);
        logger.info("获取下载已签署文件连接:{}"+JSONUtil.toJsonStr(entries));
        if (null != entries){
            DownloadSignContractInfo info = JSONUtil.toBean(JSONUtil.parseObj(entries.get("data")), DownloadSignContractInfo.class);
            return info;
        }
        return null;
    }

}
